package com.leyou.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.RegexUtils;
import com.leyou.user.dto.UserDTO;
import com.leyou.user.entity.User;
import com.leyou.user.mapper.UserMapper;
import com.leyou.user.service.UserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.print.DocFlavor;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.leyou.common.constants.MQConstants.ExchangeConstants.SMS_EXCHANGE_NAME;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public Boolean msgExist(String data, Integer type) {
        if (type != 1 && type != 2){
            throw new LyException(4000, "滚犊子");
        }
        return userService.query().eq(type == 1, "username", data)
                .eq(type == 2, "phone", data)
                .count() == 1;
    }

    @Override
    public void sndMsg(String phone) {
        if(!RegexUtils.isPhone(phone)){
            throw new LyException(5000, "手机号输入错误");
        }
        String code = RandomStringUtils.randomNumeric(6);
        redisTemplate.opsForValue().set("userCode" + phone, code,5, TimeUnit.MINUTES);
        Map<String, String> msg = new HashMap<>();
        msg.put("phone", phone);
        msg.put("code", code);
        amqpTemplate.convertAndSend("jhj", SMS_EXCHANGE_NAME, msg);
    }

    @Override
    public void userRegister(String username, String password, String phone, String code) {
        if(StringUtils.isBlank(code)){
            throw new LyException(1000, "验证码不合法");
        }

        if (! StringUtils.isBlank(redisTemplate.opsForValue().get("userCode" + phone))){
            if(! redisTemplate.opsForValue().get("userCode" + phone).equals(code)){
                throw new LyException(20000, "验证码不正确");
            }
        }else {
            throw new LyException(40000, "验证码过期");
        }

        if(! StringUtils.isBlank(username)){
            if (userService.query().eq("username", username).count() == 1){
                throw new LyException(1999, "用户名已存在");
            }

            User user = new User(null,username, passwordEncoder.encode(password), phone);
            userService.save(user);
        }
    }

    @Override
    public UserDTO quereyByUserNameAndPassword(String userName, String password) {
        if(! StringUtils.isBlank(userName) && !StringUtils.isBlank(password)){
            User user = userService.getOne(new QueryWrapper<User>().eq("username", userName));
            if(user == null){
                throw new LyException(6000, "用户名错误");
            }

            boolean bool = passwordEncoder.matches(password, user.getPassword());
            if(! bool){
                throw new LyException(7000, "密码错误");
            }

            return new UserDTO(user);

        }else {
            throw new LyException(50000, "用户名或密码不合法");
        }

    }
}
